<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="spring boot," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="#spring boot使用的一些问题总结
1.JSON序列化    一般我们使用spring boot作为框架时，controller层会打上@RestController的注解，该注解我们关注里面的@ResponseBody    @ResponseBody会将返回的内容转换成json格式
常用的JSON序列化工具
    1.fastjson（阿里的扩展包）
    2.jackson（sp">
<meta property="og:type" content="article">
<meta property="og:title" content="spring boot的一些总结">
<meta property="og:url" content="http://yoursite.com/2016/11/22/spring-boot的一些总结/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="#spring boot使用的一些问题总结
1.JSON序列化    一般我们使用spring boot作为框架时，controller层会打上@RestController的注解，该注解我们关注里面的@ResponseBody    @ResponseBody会将返回的内容转换成json格式
常用的JSON序列化工具
    1.fastjson（阿里的扩展包）
    2.jackson（sp">
<meta property="og:updated_time" content="2017-01-19T07:14:13.792Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="spring boot的一些总结">
<meta name="twitter:description" content="#spring boot使用的一些问题总结
1.JSON序列化    一般我们使用spring boot作为框架时，controller层会打上@RestController的注解，该注解我们关注里面的@ResponseBody    @ResponseBody会将返回的内容转换成json格式
常用的JSON序列化工具
    1.fastjson（阿里的扩展包）
    2.jackson（sp">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://yoursite.com/2016/11/22/spring-boot的一些总结/"/>

  <title> spring boot的一些总结 | 个人博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">个人博客</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">随便写点什么</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                spring boot的一些总结
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-11-22T16:52:17+08:00" content="2016-11-22">
              2016-11-22
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/spring-boot/" itemprop="url" rel="index">
                    <span itemprop="name">spring boot</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/11/22/spring-boot的一些总结/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/11/22/spring-boot的一些总结/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>#spring boot使用的一些问题总结</p>
<p>1.JSON序列化<br>    一般我们使用spring boot作为框架时，controller层会打上@RestController的注解，该注解我们关注里面的@ResponseBody<br>    @ResponseBody会将返回的内容转换成json格式</p>
<pre><code>常用的JSON序列化工具
    1.fastjson（阿里的扩展包）
    2.jackson（spring默认的实现）

场景：
    有一个字段：isNew .序列化时需要转换成new   
    解决方案：@JsonProperty(&quot;new&quot;)  利用jackson修改某一字段在序列化时显示的字段名
    @JsonProperty(&quot;firstName&quot;) 
    public String _first_name; 
    序列化后：{&quot;firstName&quot;:&quot;xxx&quot;}

场景：
    返回时间戳需要format一下
    解决方案：@JsonFormat(pattern=&quot;yyyy-MM-dd&apos;T&apos;hh:mm:ss&quot;) 规则跟SimpleDateFormat一样

顺便列举几个比较常用的jackson注解：
    @JsonProperty() 通常用来转换字段名在序列化后的显示
    @JsonIgnore 忽略相应字段在序列的时候不显示 
    @JsonFormat(）格式化相应字段的值
</code></pre><a id="more"></a>
<p>2.spring boot中的AOP配置<br>    首先在application.properties中设置 spring.aop.auto=true<br>    最简单的做法：<br>        1.定义需要进行切面的范围（这里是指所有com.bifong.cloud.controller包下，以Controller结尾的类中的所有方法）<br>        @Pointcut(“execution(<em> com.bifong.cloud.controller.</em>Controller.*(..))”)<br>        public void execService() {<br>        }</p>
<pre><code>    2.定义切面方法
    <figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Around</span>(<span class="string">"execService()"</span>)</div><div class="line"><span class="function"><span class="keyword">public</span> Object <span class="title">doAround</span><span class="params">(ProceedingJoinPoint pjp)</span></span>&#123;</div><div class="line">    ...</div><div class="line">    <span class="comment">//result是被切方法的原返回值</span></div><div class="line">    Object result = pjp.proceed();</div><div class="line">    ...</div><div class="line">    <span class="keyword">return</span> result</div><div class="line">&#125;</div></pre></td></tr></table></figure>

我们当时是需要在程序返回时做一些加密工作，初始是打算放在拦截器里做，后来由于一些原因（我们的数据不是放在modelAndView里的），无法实现，所以考虑到了aop切面，来实现加密过程
需要说明的是，加密过后我们返回的是一个String，但是如果被加密的result本来是一个MyObject(自定义对象)，那么spring会抛出类型转换异常，无法转换成原来的类型，原因我退出是这样的：
    controller返回，由于有RestContrller的修饰，返回会被序列化成json形式，而这里进行切面，切完后返回了一个加密string，这个返回是发生在controller层return前的，所以抛出转换异常。

<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> MyObject <span class="title">demo</span><span class="params">()</span></span>&#123;</div><div class="line">    ...</div><div class="line">    <span class="comment">//在这里return前发生切面结果的return，切面result无法转换到obj，抛出异常</span></div><div class="line">    <span class="keyword">return</span> obj;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</code></pre><p>3.spring boot在不同模式下使用不同的配置文件<br>    一般我们会使用application.properties来作为spring boot的配置文件，不过这样做的缺点就是，区分线上与线下环境的时候，需要频繁的修改这个配置（当然，如果我们启用类似consul这样的动态配置方式的话除外），这样就有可能会导致配置混淆。</p>
<pre><code>解决方案：
    我们新建两个文件application-dev.properties，application-prod.properties分别配置测试以及线上的环境
    dev文件里包含一条配置profile = dev_environment 
    prod文件里包含一条配置 profile = prod_environment（这个属性配不配其实无所谓，因为选取文件是根据文件名来匹配的，比如spring.profiles.active=prod，则会去找一个application-prod.properties的文件）
    然后在application.propertie里使用spring.profiles.active=prod 配置来启用哪个配置文件
    以上面为例：配置读取方式为，优先读取prod，如果需要的配置参数在prod里没有找到，再去application.properties去找
</code></pre><p>4.lucene更新索引<br>    lucune在更新索引后，建议reOpen一下，重新打开一下索引，否则被更新的索引不会体现出来</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/spring-boot/" rel="tag">#spring boot</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/09/21/logback-configuration/" rel="next" title="logback-configuration">
                <i class="fa fa-chevron-left"></i> logback-configuration
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/01/19/hello-world/" rel="prev" title="Hello World">
                Hello World <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/11/22/spring-boot的一些总结/"
           data-title="spring boot的一些总结" data-url="http://yoursite.com/2016/11/22/spring-boot的一些总结/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/touxiang.jpg"
               alt="钱伟峰" />
          <p class="site-author-name" itemprop="name">钱伟峰</p>
          <p class="site-description motion-element" itemprop="description">每天进步一点点</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">21</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">7</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <p class="post-toc-empty">此文章未包含目录</p>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">钱伟峰</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"qianweifeng"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  

</body>
</html>
